package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckgroupDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CheckgroupServicelmpl  implements CheckgroupService {
    @Autowired
    private CheckgroupDao checkgroupDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = checkgroupDao.pageQuery(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }
    /*
     * 新增分组
     * */
    @Override
    @Transactional
    public void add(CheckGroup checkGroup,List<String> checkitemIds) {
        log.info("检查组-添加");
        //添加分组信息
        checkgroupDao.add(checkGroup);

        Integer id = checkGroup.getId();
        for (String checkitemId : checkitemIds) {
            checkgroupDao.addGroupItm(id,checkitemId);
        }
    }
    @Override
    @Transactional
    public void delete(Integer id) {
        log.info("[检查组项-根据id删除]id：{}",id);
        //TODO 校验是否可以删除
        checkgroupDao.deleteByid(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, List<String> checkitemIds) {
        checkgroupDao.update(checkGroup);
        //然后删除之前的项目 重新添加
        Integer id = checkGroup.getId();
        checkgroupDao.delectcheckitem(id);
        //重新添加项目
        for (String checkitemId : checkitemIds) {
            checkgroupDao.addGroupItm(id,checkitemId);
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组项-根据ID查询]id:{}",id);
        return checkgroupDao.selectById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkgroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        log.info("[检查组-查询所有（套餐中）]~");
        return checkgroupDao.selectAll();
    }


}
